<template>
  <div class="roles">
    <breadcrumb :first="'权限管理'"></breadcrumb>
    <el-card>
      <el-button
        class="addUserBtn"
        @click="(dialogVisible = true), (isflag = true)"
        type="primary"
        size="small"
        >添加角色</el-button
      >
      <el-table :data="rolelist" height="410" border style="width: 100%" stripe>
        <el-table-column prop="id" label="#" width="50"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column prop="level" label="操作" width="300">
          <template slot-scope="scope">
            <el-button
              @click="changerole(scope.row)"
              type="primary"
              size="small"
              icon="el-icon-edit"
              >编辑</el-button
            >
            <el-button
              @click="delrole(scope.row)"
              type="danger"
              size="small"
              icon="el-icon-delete"
              >删除</el-button
            >
            <el-button
              @click="openRoleDialog(scope.row)"
              type="warning"
              size="small"
              icon="el-icon-s-tools"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      :title="isflag ? '添加角色' : '修改角色'"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-form :ref="role" :model="role" label-width="80px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="role.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="role.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="(dialogVisible = false), (role = {})"
          >取 消</el-button
        >
        <el-button type="primary" @click="addOrEdit">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="分配权限"
      :visible.sync="roleDialog"
      @close="checkedKeys = []"
    >
      <el-tree
        show-checkbox
        node-key="id"
        ref="power"
        :data="powerlist"
        :props="defaultProps"
        default-expand-all
        :default-checked-keys="checkedKeys"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roleDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  rightsList,
  getroleList,
  addrole,
  deleterole,
  editrole,
  rolepower,
} from "@/api/index";
import breadcrumb from "@/components/breadcrumb.vue";
export default {
  data() {
    return {
      rolelist: [],
      dialogVisible: false,
      roleDialog: false,
      role: {},
      isflag: true,
      powerlist: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      checkedKeys: [],
      roleId: "",
    };
  },
  components: {
    breadcrumb,
  },
  created() {
    this.getrole();
  },
  methods: {
    //获取列表
    getrole() {
      getroleList().then((res) => {
        this.rolelist = res.data.data;
      });
    },
    //判断对话框添加或修改功能
    addOrEdit() {
      if (this.isflag) {
        this.addRole();
      } else {
        this.changeRoleData();
      }
    },
    //修改信息对话框
    changerole(row) {
      this.isflag = false;
      this.dialogVisible = true;
      this.role = row;
    },
    // 添加用户
    addRole() {
      addrole(this.role).then((res) => {
        console.log(res.data);
        if (res.data.meta.status !== 201)
          return this.$message.error(res.data.meta.msg);
        this.$message({
          message: "添加成功",
          type: "success",
        });
      });
      this.dialogVisible = false;
      this.getrole();
    },
    //修改用户
    changeRoleData() {
      editrole(this.role).then((res) => {
        this.dialogVisible = false;
        if (res.data.meta.status !== 200)
          return this.$message.error(res.data.meta.msg);
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.getrole();
      });
    },
    //删除用户
    delrole(row) {
      deleterole(row.id).then((res) => {
        if (res.data.meta.status !== 200)
          return this.$message.error(res.data.meta.msg);
        this.$message({
          message: "删除成功",
          type: "success",
        });
        this.getrole();
      });
    },
    //分配权限对话框
    openRoleDialog(data) {
      //存储当前列角色ID
      this.roleId = data.id;
      // 调用getroleID方法取得当前角色以选择的三级ID
      this.getroleID(data, this.checkedKeys);
      //获取树形结构所有权限列表
      rightsList("tree").then((res) => {
        this.powerlist = res.data.data;
      });
      this.roleDialog = true;
    },
    //提交权限修改
    submitRole() {
      //通过内置组件方法获取已选中的ID值，并展开合并为一个数组
      const x = [
        ...this.$refs.power.getCheckedKeys(),
        ...this.$refs.power.getHalfCheckedKeys(),
      ];
      // 根据接口要求将数组转换成字符串格式并用,隔开
      const idStr = x.join(",");
      // 发起权限授权请求
      rolepower(this.roleId, idStr).then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error(res.data.meta.msg);
        }
        this.getrole();
        this.$message({
          message: "授权成功",
          type: "success",
        });
      });
      this.roleDialog = false;
      // this.checkedKeys = []
    },
    //获取当前角色所有三级ID
    getroleID(data, arr) {
      //如果当前data节点不包含children属性，则是三级节点
      if (!data.children) {
        return arr.push(data.id);
      }
      //包含节点的情况下再次调用该方法再次遍历，直到第三级节点
      data.children.forEach((item) => {
        this.getroleID(item, arr);
      });
    },
  },
};
</script>

<style lang="less" scoped>
.roles {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  padding: 10px;
  background-color: #fafafa;
  padding: 10px;
  .addUserBtn {
    margin-bottom: 10px;
  }
}
</style>
